package com.yls.except.dao;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/12.
 */
@Repository(value = "indentDao")
public interface IndentDao {

    /**
     * 新增订单信息
     *
     * @param indentinfo 订单信息
     * @return 新增状态（大于0成功，否则失败）
     */
    @Insert("insert into yls_indent(indent_num,money,freight_money,paymoney,indent_state,uid,type,address_id,addtime,addip,paytime,paytype,isdisabled,favorable,typeid) " +
            "values " +
            "(#{indent_num},#{indentmoney},#{freight},#{totalmoney},#{indent_state},#{uid},#{type},#{address_id},#{addtime},#{addip},#{paytime},#{paytype},#{isdisabled},#{favorable},#{typeid})")
    int insertIndentInfo(Map<String, Object> indentinfo);

    /**
     * 新增订单商品信息
     *
     * @param indentshop 订单商品信息
     * @return 新增状态（大于0成功，否则失败）
     */
    @Insert("insert into yls_indentshop " +
            "(indent_shop_id,indent_num,shop_id,num,baby_sex,baby_age_id,shop_spec_price,addtime,addip,isdisabled) " +
            "values" +
            "(#{indent_shop_id},#{indent_num},#{shop_id},#{num},#{baby_sex},#{baby_age_id},#{shop_spec_price},#{addtime},#{addip},#{isdisabled})")
    int insertIndentShopInfo(Map<String, Object> indentshop);

    /**
     * 根据订单编号查询订单信息
     *
     * @param indentnum 订单编号
     * @return 订单信息
     */
    @Select("select " +
            "indent_num,uid,money,freight_money,paymoney,type,address_id,indent_state,sendtime,recipienttime,addtime,addip,paytype,paytime,return_num,return_status,isdisabled,favorable,typeid " +
            "from " +
            "yls_indent where indent_num=#{indentnum}")
    Map<String, Object> selectIndentInfoByIndentNum(String indentnum);

    /**
     * 修改订单信息
     *
     * @param indentinfo
     * @return
     */
    @Update("update yls_indent set " +
            "uid=#{uid},money=#{money},freight_money=#{freight_money},paymoney=#{paymoney},type=#{type},address_id=#{address_id},indent_state=#{indent_state},sendtime=#{sendtime}," +
            "recipienttime=#{recipienttime},addtime=#{addtime},addip=#{addip},paytype=#{paytype},paytime=#{paytime},return_num=#{return_num},return_status=#{return_status}," +
            "isdisabled=#{isdisabled},favorable=#{favorable},typeid=#{typeid} " +
            "where " +
            "indent_num=#{indent_num}")
    int updateIndentInfo(Map<String, Object> indentinfo);

    /**
     * 根据用户id和订单状态查询订单列表信息
     *
     * @param map 用户id和订单状态
     * @return 订单列表信息
     */
    @Select("select indent_num,money,freight_money,paymoney,indent_state,sendtime,recipienttime,addtime,favorable from yls_indent " +
            "where uid=#{uid} and isdisabled=1 ${indentstate} ${type} order by addtime desc limit #{start},#{count}")
    List<Map<String, Object>> selectIndentInfoByUidAndIndentState(Map<String, Object> map);

    /**
     * 根据订单编号查询订单商品信息
     *
     * @param indentnum 订单编号
     * @return 订单商品信息
     */
    @Select("select i.indent_shop_id,i.indent_num,i.shop_id,i.num,i.baby_sex,i.baby_age_id,i.shop_spec_price,s.shop_name,spec.`name`,price.img,price.price " +
            "from yls_indentshop i,yls_shopping s,yls_shop_spec spec, yls_shop_spec_price price " +
            "where indent_num=#{indentnum} and i.shop_id=s.shop_id and i.shop_spec_price=price.shop_spec_price_id and s.shop_id=price.shop_id and spec.spec_id=price.spec_id")
    List<Map<String, Object>> selectIndentShopInfoByIndentNum(String indentnum);

    /**
     * 根据用户id和订单编号查询发货通知信息
     *
     * @param map 用户id和订单编号
     * @return 发货通知信息
     */
    @Select("select uid,indentnum,addtime,addip,isdisabled from yls_sendadvice where uid=#{uid} and indentnum=#{indentnum}")
    Map<String, Object> selectSendAdviceInfoByIndentNumAndUid(Map<String, Object> map);

    /**
     * 新增发货通知信息
     *
     * @param map 发货通知信息
     * @return 发货通知信息
     */
    @Insert("insert into yls_sendadvice (uid,indentnum,addtime,addip,isdisabled) " +
            "values " +
            "(#{uid},#{indentnum},#{addtime},#{addip},#{isdisabled})")
    int insertSendAdvice(Map<String, Object> map);

    /**
     * 修改发货通知信息
     *
     * @param sendadvice 发货通知信息
     * @return 修改状态（大于0成功，否则失败）
     */
    @Update("update yls_sendadvice set addtime=#{addtime},addip=#{addip},isdisabled=#{isdisabled} where uid=#{uid} and indentnum=#{indentnum}")
    int updateSendAdvice(Map<String, Object> sendadvice);

    /**
     * 根据订单编号查询物流信息
     *
     * @param indentnum 订单编号
     * @return 物流信息
     */
    @Select("select logistics_id,logistics_name,logistics_num from yls_logistics where indent_num=#{indentnum} and isdisabled=1")
    Map<String, Object> selectLogisticsByIndentNum(String indentnum);

    /**
     * 根据用户id和商品id和商品规格价格id查询用户仓储信息
     *
     * @param repertory
     * @return
     */
    @Select("select repertory_id,uid,shop_id,num,shop_spec_price,sendnum,isdisabled from yls_repertory where " +
            "uid=#{uid} and shop_id=#{shop_id} and shop_spec_price=#{shop_spec_price}")
    Map<String, Object> selectReperToryInfoByUidAndShopidAndShopSpecPriceId(Map<String, Object> repertory);

    /**
     * 新增用户仓储信息
     *
     * @param repertory 仓储信息
     * @return 仓储信息
     */
    @Insert("insert into yls_repertory (repertory_id,uid,shop_id,num,shop_spec_price,sendnum,isdisabled) " +
            "values (#{repertory_id},#{uid},#{shop_id},#{num},#{shop_spec_price},#{sendnum},#{isdisabled})")
    int insertRepertory(Map<String, Object> repertory);

    /**
     * 修改用户仓储信息
     *
     * @param repertoryinfo 仓储信息
     * @return 仓储信息
     */
    @Update("update yls_repertory set uid=#{uid},shop_id=#{shop_id},num=#{num},shop_spec_price=#{shop_spec_price},sendnum=#{sendnum},isdisabled=#{isdisabled} where repertory_id=#{repertory_id}")
    int updateRepertory(Map<String, Object> repertoryinfo);

    /**
     * 根据用户id查询用户仓储信息
     *
     * @param map 用户id及分页信息
     * @return
     */
    @Select("select s.shop_id,s.shop_name,sp.`name`,pr.img,pr.price,r.num,r.repertory_id,r.sendnum,r.shop_spec_price " +
            "from yls_repertory r,yls_shopping s,yls_shop_spec sp,yls_shop_spec_price pr " +
            "where r.uid=#{uid} and r.num>r.sendnum and s.shop_id=r.shop_id and r.shop_spec_price=pr.shop_spec_price_id and pr.spec_id=sp.spec_id " +
            "order by r.num desc limit #{start},#{count}")
    List<Map<String, Object>> selectRepertoryInfoByUid(Map<String, Object> map);

    /**
     * 根据仓储id查询仓储信息
     *
     * @param repertoryid 仓储id
     * @return 仓储信息
     */
    @Select("select s.shop_name,sp.`name`,pr.img,pr.price,r.num,r.repertory_id,r.sendnum,s.shop_weight,s.expired " +
            "from yls_repertory r,yls_shopping s,yls_shop_spec sp,yls_shop_spec_price pr " +
            "where r.repertory_id=#{repertoryid} and s.shop_id=r.shop_id and r.shop_spec_price=pr.shop_spec_price_id and pr.spec_id=sp.spec_id")
    Map<String, Object> selectReperToryInfoById(String repertoryid);

    @Update("update yls_repertory set sendnum=#{sendnum} where repertory_id=#{repertory_id}")
    int updateRepertorySend(Map<String, Object> repertoryinfo);

    /**
     * 根据订单类型id查询订单信息
     *
     * @param typeid 订单类型id
     * @return 订单信息
     */
    @Select("select indent_num,money,freight_money,paymoney,indent_state,sendtime,recipienttime,addtime,favorable from yls_indent where typeid=#{typeid}")
    List<Map<String, Object>> selectIndentInfoByTypeId(String typeid);

    /**
     * 修改仓储订单信息
     *
     * @param indent 仓储订单信息
     * @return 修改状态（大于0成功，否则失败）
     */
    @Update("update yls_indent set indent_state=#{indent_state},paytime=#{paytime},paytype=#{paytype},return_num=#{return_num} where typeid=#{typeid} and indent_state=1")
    int updateRepertoryIndentInfo(Map<String, Object> indent);

    /**
     * 获取仓储发货订单信息
     * @param map
     * @return
     */
    @Select("select indent_num,money,freight_money,paymoney,indent_state,sendtime,recipienttime,addtime,favorable,typeid from yls_indent where uid=#{uid} ${type} ${indentstate} group by typeid order by addtime desc limit #{start},#{count} ")
    List<Map<String,Object>> selectRepertoryIndentInfoByUidAndIndentState(Map<String, Object> map);

    /**
     * 根据仓储订单id查询仓储信息
     * @param typeid 仓储订单id
     * @return 仓储信息
     */
    @Select("select indent_num,money,freight_money,paymoney,indent_state,sendtime,recipienttime,addtime,favorable from yls_indent where typeid=#{typeid}")
    List<Map<String,Object>> selectRepertoryIndentInfoByTypeid(String typeid);

    /**
     * 根据用户id查询用户订单列表信息
     * @param uid 用户id
     * @return 用户订单列表信息
     */
    @Select("select indent_num from yls_indent where uid=#{uid} and isdisabled=1 and indent_state in (3) and type in (0,3)")
    List<Map<String,Object>> selectIndentListInfoByUid(String uid);

    /**
     * 修改自动确认收货订单
     * @return
     * @param recipienttime 发货时间
     * @param time 当前时间
     */
    @Update("update yls_indent set recipienttime=#{time},indent_state=4 where sendtime<=#{recipienttime} and indent_state=3 and isdisabled=1")
    int updateAutoConfirmIndentByTime(@Param("recipienttime") long recipienttime, @Param("time") long time);

    /**
     * 根据时间查询发货订单信息
     * @param recipienttime 时间
     * @return 订单信息
     */
    @Select("select sendtime from yls_indent where sendtime>#{recipienttime} and indent_state=3 and isdisabled=1 limit 0,1")
    Map<String,Object> selectIndentinfoNotConfirmIndentByTime(long recipienttime);

    /**
     * 修改自动取消订单信息
     * @param cancletime 取消时间
     * @param time 当前时间
     * @return
     */
    @Update("update yls_indent set indent_state=5 where addtime<=#{cancletime} and indent_state=1 and isdisabled=1")
    int updateAutoCancleIndentByTime(@Param("cancletime") long cancletime, @Param("time") long time);

    /**
     * 获取即将自动取消订单
     * @param cancletime 取消时间
     * @return
     */
    @Select("select addtime from yls_indent where addtime>#{cancletime} and indent_state=1 and isdisabled=1 limit 0,1")
    Map<String,Object> selectIndentinfoNotCancleIndentByTime(long cancletime);
}